package algorithm.easy;

/**
 * @author 江岸
 * @version V1.0
 * @ClassName: FindMaxAverage643
 * @description: 给定 n 个整数，找出平均数最大且长度为 k 的连续子数组，并输出该最大平均数。
 * 输入：[1,12,-5,-6,50,3], k = 4
 * 输出：12.75
 * 解释：最大平均数 (12-5-6+50)/4 = 51/4 = 12.75
 * @date 2021/2/417:09
 * @since V1.0
 */
public class FindMaxAverage643 {

    public static void main(String[] args) {
        int[] nums = {1,12,-5,-6,50,3};
        findMaxAverage(nums,4);
    }

    //滑动窗口
    public static double findMaxAverage(int[] nums, int k) {
        int right = k;
        int avg = 0;
        int max = 0;
        for (int i=0;i<k;i++){
            avg = avg+ nums[i];
        }
        max = avg;
        while(right<nums.length){
            avg =   (nums[right]-nums[right-k]) + avg;
            max = Math.max(max,avg);
            right++;
        }
        return (double) max/k;
    }
}
